<template>
    <div class="modbus-wrapper">
        <Block>
            <DataGrid :auto-load="autoLoad" ref="dataGridRef">
                <template #toolbar-search>
                    <div class="query-from-box">
                        <el-form>
                            <el-form-item>
                                <el-input v-model="queryData.modelAttrName" placeholder="命令名称" clearable />
                            </el-form-item>
                            <el-form-item>
                                <el-input v-model="queryData.protocolClass" placeholder="命令分类" clearable />
                            </el-form-item>
                        </el-form>
                    </div>
                </template>
                <el-table-column type="index" label="序号" width="60" align="center" fixed />
                <el-table-column
                    prop="commandDescription"
                    label="命令名称"
                    fixed
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column prop="commandType" label="命令分类" show-overflow-tooltip />
                <el-table-column label="是否启用脚本">
                    <template #default="{ row }">
                        <el-switch v-model="row.enableScript" disabled />
                    </template>
                </el-table-column>
                <el-table-column label="报文模版/脚本名称" show-overflow-tooltip>
                    <template #default="{ row }">
                        <div class="table-item-multiple-line">
                            <el-link v-if="row.enableScript" type="primary" @click="onViewScript(row)">
                                {{ row.script ? row.script.scriptName : '' }}
                            </el-link>
                            <template v-else>
                                <el-link
                                    v-for="item in row.templates"
                                    :key="item.templateNo"
                                    type="primary"
                                    @click="onViewItem(item)"
                                >
                                    {{ item.templateName }}
                                </el-link>
                            </template>
                        </div>
                    </template>
                </el-table-column>
            </DataGrid>
        </Block>
    </div>
</template>
<script setup lang="ts">
import { IoTProtocolService, runScriptService } from '@great/services';
import { DataGrid, Block, type DataGridInstance } from '@great/components';
import { showPopupBox } from '@great/utils';
import MessageModal from './MessageModal.vue';
import DialogModal from '@/pages/run/scriptManage/components/DialogModal.vue';

const protocolDetail = inject<any>('protocolDetail').value;
// 查询条件
const queryData = ref({
    protocolNo: protocolDetail.protocolPublic.protocolNo,
    modelAttrName: '',
    protocolAttrKey: '',
    protocolClass: ''
});
const dataGridRef = useTemplateRef<DataGridInstance>('dataGridRef');
const autoLoad = reactive({
    debounce: true,
    reloadDeps: [queryData],
    onLoad: async (current, size) => {
        const res = await IoTProtocolService.getPrivatePage({
            current,
            size,
            ...queryData.value
        });
        return { data: res.data.records, total: res.data.total };
    }
});

// 查看详情
const onViewItem = (row: { id: string }) => {
    showPopupBox(MessageModal, {
        row: row
    });
};
// 查看脚本
const onViewScript = async (row: any) => {
    let res = await runScriptService.getScriptDetail(row.script.id);
    showPopupBox(DialogModal, {
        row: res.data,
        readonly: true
    });
};
</script>

<style scoped lang="scss">
.modbus-wrapper {
    .query-from-box {
        display: flex;
        align-items: center;
        .el-form {
            display: flex;
            .el-form-item {
                margin-bottom: 0;
                width: 192px;
            }
            .el-form-item + .el-form-item {
                margin-left: 10px;
            }
        }
    }
    :deep(.data-grid) {
        .table-item-multiple-line {
            display: flex;
            flex-direction: column;
            align-items: flex-start;
        }
    }
}
</style>
